package com.fangcun.controller.api.old.agent.goods;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fangcun.bean.sys.ResponseMsg;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProAffairException;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.util.PublicMethod;
import com.fangcun.common.util.apiversion.ApiVersion;
import com.fangcun.common.util.base.BaseController;
import com.fangcun.entity.AgentGoods;
import com.fangcun.service.api.AgentGoodsService;

/**
 * @作者： 陳曉鬆 @时间： 2020年8月16日 - 上午9:21:01 @description：->
 */
@RestController
@RequestMapping("/api/{version}/agent/goods")
public class AgentGoodsController extends BaseController {

	private static Logger LOG = LoggerFactory.getLogger(AgentGoodsController.class);

	@Resource(name = "agentGoodsService")
	private AgentGoodsService agentGoodsService;

	/** 一键转链 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/auth/add")
	private ResponseMsg add(@RequestBody List<AgentGoods> request) throws ProException {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			agentGoodsService.agentChainChange(request, getApiUserId());
		} catch (ProAffairException e) {
			LOG.error("-代理商商品-转链出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-代理商商品-转链异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 取消一键转链 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/auth/update/status")
	private ResponseMsg update(@RequestBody List<AgentGoods> request) throws ProException {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			agentGoodsService.agentCancelChainChange(request, getApiUserId());
		} catch (ProAffairException e) {
			LOG.error("-代理商商品-取消转链出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-代理商商品-取消转链异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
}
